﻿using QD.LopPlat.UI.Code;
using QD.LopPlat.UI.Code.DbContent;
using QD.LopPlat.UI.Model.Sys;
using QD.Tools;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace QD.LopPlat.UI
{
    public static class GeneralHelper
    {
        /// <summary>
        /// 按系统默认规则生成编号
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="numLength"></param>
        /// <param name="curNum"></param>
        /// <returns></returns>
        public static string GetInitCode(string prefix)
        {
            Random rd = new Random();
            int d = rd.Next(10, 100);

            string code = GetInitCode(prefix, d);
            return code;
        }

        /// <summary>
        /// 按系统默认规则生成编号
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="numLength"></param>
        /// <param name="curNum"></param>
        /// <returns></returns>
        public static string GetInitCode(string prefix, int step)
        {
            string str = string.Format("{0}{1}", DateTime.Now.ToString("yyMMddHHmmss"), step.ToString());
            string code = prefix + str;
            return code;
        }

        /// <summary>
        /// 取所有组织
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAllQf(bool addEmpty = true)
        {
            DbQfDevPlat db = new DbQfDevPlat();
            IList<QDDataBase> ftList = db.QDDataBase.ToList();
            DataTable dt = ftList.Select(p => (object)p).ToList().ToDataTable(new QDDataBase());

            if (addEmpty)
            {
                DataRow dr = dt.NewRow();
                dr["QfCode"] = "";
                dr["QfName"] = "";
                dt.Rows.InsertAt(dr, 0);
            }

            dt.DefaultView.Sort = "QfName asc";

            return dt;
        }

        /// <summary>
        /// 取当前组织的所有PC用户
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAllSys_Uid()
        {
            DbQfDevPlat db = new DbQfDevPlat();
            IList<Sys_UidPwd> ftList = db.Sys_UidPwd.ToList();
            DataTable dt = ftList.Select(p => (object)p).ToList().ToDataTable(new Sys_UidPwd());
            dt.DefaultView.Sort = "UserName asc";
            return dt;
        }        

        /// <summary>
        /// 取所有显示类型
        /// </summary>
        /// <returns></returns>
        public static string[] GetAllPresentType()
        {
            string[] s = new string[] { "", "puredata", "string", "date", "time", "number", "money", "bool", "seal", "picture" };
            return s;
        }

        /// <summary>
        /// 取对齐方式 
        /// </summary>
        /// <returns></returns>
        public static string[] GetAlign()
        {
            string[] s = new string[] { "left", "center", "right" };
            return s;
        }

        /// <summary>
        /// 取表单中的列类型，去掉类型中的长度信息
        /// </summary>
        /// <param name="columnType"></param>
        /// <returns></returns>
        public static string GetFormType(string columnType)
        {
            int index = columnType.IndexOf("(");
            if (index > 0)
            {
                return columnType.Substring(0, index);
            }
            else
            {
                return columnType;
            }
        }

        /// <summary>
        /// 打开一个窗口
        /// </summary>
        /// <param name="formName"></param>
        /// <param name="args"></param>
        public static Form OpenPcForm(string formName, params object[] args)
        {
            return OpenForm(formName, "QD.LopPlat.UI", args);
        }

        /// <summary>
        /// 打开一个平台管理的窗口
        /// </summary>
        /// <param name="formName"></param>
        /// <param name="args"></param>
        public static Form OpenPlatformForm(string formName, params object[] args)
        {
            return OpenForm(formName, "QD.LopPlat.UI.Platform", args);
        }

        /// <summary>
        /// 打开一个流程设计的窗口
        /// </summary>
        /// <param name="formName"></param>
        /// <param name="args"></param>
        public static Form OpenWorkFlowForm(string formName, params object[] args)
        {
            return OpenForm(formName, "QD.LopPlat.UI.Workflow", args);
        }

        /// <summary>
        /// 打开一个BUG管理的窗口
        /// </summary>
        /// <param name="formName"></param>
        /// <param name="args"></param>
        public static Form OpenBUGForm(string formName, params object[] args)
        {
            return OpenForm(formName, "QD.LopPlat.UI.Tester", args);
        }

        /// <summary>
        /// 打开一个项目管理的窗口
        /// </summary>
        /// <param name="formName"></param>
        /// <param name="args"></param>
        public static Form OpenPMForm(string formName, params object[] args)
        {
            return OpenForm(formName, "QD.LopPlat.UI.PM", args);
        }

        /// <summary>
        /// 打开一个App配置窗口
        /// </summary>
        /// <param name="formName"></param>
        /// <param name="args"></param>
        public static Form OpenAppForm(string formName, params object[] args)
        {
            return OpenForm(formName, "QD.LopPlat.UI.App", args);
        }

        private static Form OpenForm(string formName, string path, object[] args)
        {
            try
            {
                Form form = Application.OpenForms[formName];
                if (form == null)
                {
                    Assembly assembly = Assembly.Load("QDLopPlat");
                    if (args.Length > 0)
                    {
                        form = (Form)assembly.CreateInstance(path + "." + formName, false, BindingFlags.Default, null, args, null, null);
                    }
                    else
                    {
                        form = (Form)assembly.CreateInstance(path + "." + formName);
                    }
                    form.Show();
                }
                else
                {
                    if (!form.Visible)
                    {
                        form.Show();
                    }
                    form.Activate();
                }
                return form;
            }
            catch (Exception ex)
            {
                Tools.PromptHelper.Prompt(ex);
                return null;
            }
        }

        /// <summary>
        /// 从字符串中解析出数据源编号
        /// 正则表达式为：\[@(.*?)\]
        /// </summary>
        /// <returns></returns>
        public static string GetDbFromRegex(string str)
        {
            string dbCode = string.Empty;
            string fieldRegStr = @"\[@(.*?)\]";
            Regex regexField = new Regex(fieldRegStr);
            if (regexField.IsMatch(str))
            {
                Match match = regexField.Match(str);
                string[] fieldActCodes = match.Value.Replace("[@", "").Replace("]", "").Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
                if (fieldActCodes.Length != 2)
                {
                    throw new Exception("公式定义错误，参数的正确格式应该是[@数据源.字段]，参数值：" + match.Value);
                }
                else
                {
                    dbCode = fieldActCodes[0];
                }
            }

            return dbCode;
        }
    }
}
